package com.zhongwang.mms.module.jiaGongMaterial.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.TDept;
import com.zhongwang.mms.gen.entity.TWarehouseInfo;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.basic.service.MMaterialDictionaryService;
import com.zhongwang.mms.module.basic.service.TDeptService;
import com.zhongwang.mms.module.jiaGongMaterial.dao.MaterialInDao;
import com.zhongwang.mms.module.jiaGongMaterial.model.JMaterialAllModel;
import com.zhongwang.mms.module.jiaGongMaterial.model.JMaterialInsertModel;
import com.zhongwang.mms.module.jiaGongMaterial.service.MaterialInService;
import com.zhongwang.mms.module.jiaGongMaterial.service.MaterialStockService;
import com.zhongwang.mms.module.material.dao.CompositeOutcomeDao;
import com.zhongwang.mms.module.material.dao.MOrderDao;
import com.zhongwang.mms.module.material.dao.MOrderDetailDao;
import com.zhongwang.mms.module.material.model.ComplexStorageModel;
import com.zhongwang.mms.module.material.model.CompositeOutcomeModel;
import com.zhongwang.mms.module.material.service.ComplexStorageService;
import com.zhongwang.mms.module.material.service.MOrderService;
import com.zhongwang.mms.module.purchase.model.PurWarQueryModel;
import com.zhongwang.mms.util.FileUtils;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 原料库存查询 控制层
 * @Author: China.wsg
 * @Date: 2020/04/01 11:23
 */
@Controller
@RequestMapping("/materialStockQuery")
public class MaterialStockController extends BaseController {
    @Autowired
    private ComplexStorageService complexStorageService;
    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;
    @Autowired
    private MaterialStockService materialStockService;
    /**
     * 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page/{projectCode}")
    public String project(@PathVariable("projectCode") String projectCode, Model model) {

        // 页面调用位置;1列表库存查询页面（projectCode = kccx） 2项目余量查询 查询按钮（projectCode = 选中行项目编号）
        if (projectCode.equals("kccx")) {
            projectCode = "";
            model.addAttribute("ityInType", "");
        } else {
            // 项目在库类型
            model.addAttribute("ityInType", 2);
        }
        model.addAttribute("projectCode", projectCode);
        List<TWarehouseInfo> warehouseInfoList = tWarehouseInfoDao.findWarehouseInfoListByWhiType("原材料甲供件库");
        model.addAttribute("warehouse",warehouseInfoList );
        model.addAttribute("warehouseInfoList",warehouseInfoList );
        String warehouseStr = "";
        for (int i = 0; i < warehouseInfoList.size(); i++) {
            warehouseStr+=warehouseInfoList.get(i).getWhiId()+",";
        }
        if (!warehouseStr.equals("")){
            warehouseStr=warehouseStr.substring(0,warehouseStr.length()-1);
        }
        model.addAttribute("warehouseStr", warehouseStr);
        return "jiaGongMaterial/materialStock/page";
    }

    @Value("${user.upload.path}")
    private String uploadPath;
    /**
     * 列表 下载
     * @param excelFileName 下载的文件名
     * @return list
     */
    @GetMapping("/downloadExportExcel/{excelFileName}")
    @ResponseBody
    public void downloadExportExcel(@PathVariable("excelFileName") String excelFileName, HttpServletResponse response) {
        //获取当前用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String excelPath = uploadPath + user.getAccount() + "/"+excelFileName;
        FileUtils.downloadFile(excelPath, response);
    }
    /**
     * 查询原料在库明细
     *
     * @param jMaterialAllModel 查询条件
     * @param matTypeIdList       类型Id多选
     * @param matQualityIdList    材质Id多选
     * @param matSpecIdList       规格Id多选
     * @return
     */
    @PostMapping("/exportExcel")
    @ResponseBody
    public JsonResult exportExcel(JMaterialAllModel jMaterialAllModel, @RequestParam(value = "matTypeIdList[]", required = false) List<String> matTypeIdList,
                                  @RequestParam(value = "matQualityIdList[]", required = false) List<String> matQualityIdList,
                                  @RequestParam(value = "matSpecIdList[]", required = false) List<String> matSpecIdList,
                                  @RequestParam(value = "whiIdList[]", required = false) List<String> whiIdList) {
        if(whiIdList==null){
            if (jMaterialAllModel.getWhiIdListStr()!=null){
                whiIdList = Arrays.asList(jMaterialAllModel.getWhiIdListStr().split(","));
            }
        }
        return materialStockService.exportExcel(jMaterialAllModel, matTypeIdList, matQualityIdList, matSpecIdList,whiIdList);
    }
}
